package cn.bdqn.pn.controller.schedule;

import cn.bdqn.pn.pojo.schedule.Seat;
import cn.bdqn.pn.pojo.schedule.Ticketfile;
import cn.bdqn.pn.service.schedule.SeatService;
import cn.bdqn.pn.service.schedule.TicketFileService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("ticket")
public class TicketFileController {

    @Autowired
    private TicketFileService ticketFileService;

    @Autowired
    private SeatService seatService;

    /**
     * 添加票档信息
     * @param
     * @return
     */
    @RequestMapping(value = "/addTicketFileInfo")
    @ResponseBody
    public boolean addTicketFileInfo(@Param("array")int[] array,@Param("areanum")String areanum,
                                     @Param("ticketfilename") String ticketfilename,
                                     @Param("placeId")Integer placeId,
                                     @Param("color")String color){
        System.out.println("=======================array="+array);
        System.out.println("=======================areanum="+areanum);
        System.out.println("=======================ticketfilename="+ticketfilename);
        System.out.println("=======================placeId="+placeId);
        Ticketfile ticketfile=new Ticketfile();
        ticketfile.setRepertory(array.length);
        ticketfile.setAreanum(areanum);
        ticketfile.setPlaceid(placeId);
        ticketfile.setColor(color);
        ticketfile.setTicketfilename(ticketfilename);

        Seat seat=null;
        boolean falg=false;
        boolean ticketFalg=false;
        for(int i=0;i<array.length;i++){
            try {
                seat=seatService.findBySeatId(array[i]);
                if(areanum!=null){
                    seat.setAreanum(areanum);
                }
                //调用座位修改方法
                falg=seatService.updateSeatInfo(seat);
                //调用票档新增方法
                ticketFalg=ticketFileService.addTicketFile(ticketfile);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(falg&&ticketFalg){
            return true;
        }else {
            return false;
        }
    }






    /**
     * 根据场地查询票档信息
     */
    @RequestMapping(value = "/selectAllByPlaceid")
    @ResponseBody
    public  List<Ticketfile> selectAllByPlaceid(@Param("placeid") Integer placeid){
        System.out.println("id"+placeid);
        List<Ticketfile> ticketfileList=null;
       try {
          ticketfileList= ticketFileService.selectAllByPlaceid(placeid);
       }catch (Exception e){
           e.printStackTrace();
       }
        System.out.println(JSON.toJSONString(ticketfileList,true));
       return ticketfileList;
    }

}
